package com.vueapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vueapi.domain.HjCategory;
import com.vueapi.domain.HjComment;
import com.vueapi.domain.HjTag;
import com.vueapi.domain.vo.TagCountVO;
import com.vueapi.mapper.HjTagMapper;
import com.vueapi.service.HjArticleTagService;
import com.vueapi.service.HjTagService;
import com.vueapi.utils.DateUtils;
import com.vueapi.utils.ParseUtils;
import com.vueapi.utils.ResultUtils;
import com.vueapi.utils.TimeAgoUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 15442
 * @description 针对表【hj_tag】的数据库操作Service实现
 * @createDate 2022-09-20 11:44:51
 */
@Service
@Slf4j
public class HjTagServiceImpl extends ServiceImpl<HjTagMapper, HjTag>
        implements HjTagService {

    @Autowired
    private HjArticleTagService articleTagService;

    @Override
    public ResultUtils listPage(Integer pageNum, Integer pageSize, String tagSearch) {
        Page<HjTag> tagPage = page(new Page<>(pageNum, pageSize), new LambdaQueryWrapper<HjTag>()
                .eq(HjTag::getIsDeleted, 0)
                .like(HjTag::getName, tagSearch)
                .orderByDesc(HjTag::getSort)
                .orderByDesc(HjTag::getCreateTime));
        return ResultUtils.success(tagPage);
    }

    @Override
    public ResultUtils top(HjTag tag) {
        HjTag one = getOne(new LambdaQueryWrapper<HjTag>().orderByDesc(HjTag::getSort).last("limit 1"));
        if (tag.getSort() < one.getSort()) {
            tag.setSort(one.getSort() + 1);
        }
        updateById(tag);
        return ResultUtils.success("置顶成功", null);
    }

    @Override
    public void updateArticleCount(Integer id) {
        this.baseMapper.updateArticleCount(id);
    }

    @Override
    public List<TagCountVO> getTagCount() {
        return list(new LambdaQueryWrapper<HjTag>().select(HjTag::getName, HjTag::getArticleCount).last("limit 10"))
                .stream()
                .map(t -> new TagCountVO(t.getName(), t.getArticleCount()))
                .collect(Collectors.toList());
    }

    @Override
    public List<Integer> getTagAddCount() {
        ArrayList<Integer> dataCountVOS = new ArrayList<>();
        for (int i = 7; i >= 1; i--) {

            Date today = TimeAgoUtils.getTodayZeroTimePre(i);
            Date lastDay = null;
            // 0代表了今天，那么就要算今天到明天，所以要调用往后加天数的方法 暂时取消
            lastDay = TimeAgoUtils.getTodayZeroTimePre(i-1);

            LambdaQueryWrapper<HjTag> between = new LambdaQueryWrapper<HjTag>().select(HjTag::getId).between(HjTag::getCreateTime,today, lastDay);
            dataCountVOS.add((int) count(between));
        }
        return dataCountVOS;
    }

    @Override
    public Integer getCount() {
        return this.baseMapper.getCount();
    }

    @Override
    public ResultUtils getTags() {
        LambdaQueryWrapper<HjTag> wrapper = new LambdaQueryWrapper<HjTag>().select(HjTag::getId, HjTag::getName);
        List<HjTag> tagList = list(wrapper);
        return ResultUtils.success(tagList);
    }

    @Override
    public List<String> checkAndRemoveByIds(String ids) {
        log.info("=========================Remove Tag Start=========================");
        // 如果有文章关联此分类就不能删除
        // 查询已经被使用的分类
        List<String> useTagIds = articleTagService.getAllUseTagByIds(ParseUtils.intArrayToList(ParseUtils.stringToIntArray(ids)));
        if(useTagIds.isEmpty()){
            // 集合为空，直接循环删除
            this.removeByIds(Arrays.asList(ids.split(",")));
            log.info("没有正在使用的 tag...");
        }
        log.info("当前正在使用的 tag：" + useTagIds);
        log.info("=========================Remove Tag End=========================");
        return useTagIds;
    }
}




